#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);

#include <vtkSmartPointer.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageCast.h>
#include <vtkImageData.h>
#include <vtkImageShiftScale.h>
#include <vtkImageWriter.h>
#include <vtkImageWriter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageViewer2.h>
#include <iostream>

int main(int argc, char* argv[])
{

    // 读取 DICOM 文件
    vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
    reader->SetDirectoryName("D:\\CT\\IMP-KB-F1");
    reader->Update();

    vtkSmartPointer<vtkImageData> imageData = reader->GetOutput();

    // 显示原始图像
    vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
    viewer->SetInputData(imageData);
    viewer->SetColorLevel(128); // 调整色彩级别
    viewer->SetColorWindow(256); // 调整色彩窗口

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    viewer->SetupInteractor(renderWindow->GetInteractor());
    viewer->Render();
    viewer->GetRenderWindow()->SetSize(800, 600);
    viewer->Render();
    viewer->GetRenderWindow()->Render();

    // 修改像素值，使用 vtkImageShiftScale
    vtkSmartPointer<vtkImageShiftScale> shiftScale = vtkSmartPointer<vtkImageShiftScale>::New();
    shiftScale->SetInputData(imageData);
    shiftScale->SetShift(0); // 不改变偏移量
    shiftScale->SetScale(2); // 将像素值乘以 2
    shiftScale->SetOutputScalarTypeToUnsignedShort(); // 设置输出类型
    shiftScale->Update();

    vtkSmartPointer<vtkImageWriter> writer = vtkSmartPointer<vtkImageWriter>::New();
    writer->SetFileName("D:\\CT\\IMP-KB-F2");
    writer->SetInputData(shiftScale->GetOutput());
    writer->Write();


    std::cout << "Modified DICOM file saved as " << "D:\\CT\\IMP-KB-F2" << std::endl;

    // 启动交互器
    vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    interactor->SetRenderWindow(renderWindow);
    interactor->Start();

    return EXIT_SUCCESS;
}
